﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class adminSubmitPrice : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CH", 1))
                {
                    ddlCompanyBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void ddlCompanyBind()
        {
            DataTable dt = logic.company.listSellerWithSellerRange();
            ddlCompany.DataSource = dt;
            ddlCompany.DataBind();
            ddlCompany.Items.Insert(0,new ListItem("-请选择供应商-","-1"));
        }

        public int companyId
        {
            set
            {
                companyId = value;
            }
            get
            {
                int _companyId = -1;
                int.TryParse(ddlCompany.SelectedValue, out _companyId);
                return _companyId;
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append(" and 1=1");
            if (txt_proName.Text.Trim() != "")
                where.AppendFormat(" and productName like '%{0}%'", txt_proName.Text.Trim());
            if (companyId != -1)
            {
                DataSet ds = logic.sellerRange.sellerRangelistWithPrice(companyId, where.ToString(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                rpt_list.DataSource = ds.Tables[1];
                rpt_list.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void btnAnalysis_Click(object sender,EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }

        protected void rpt_list_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (companyId!=-1)
                {
                    DataRowView rowView = e.Item.DataItem as DataRowView;
                    int productId = Convert.ToInt32(rowView["productId"]);
                    Repeater rpt = e.Item.FindControl("rpt_Seller_Price") as Repeater;

                    DataTable dt = logic.price.listWithBusinessNatures(productId, companyId);
                    rpt.DataSource = dt;
                    rpt.DataBind(); 
                }
            }
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_proName.Text = "";
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
    }
}
